package com.andnnl.fileanalysis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * txt文件读取，目前固定为逗号分隔符
 * Created by chenss on 2021/6/2.
 */
public class TxtAnalysis extends AbsFileAnalysis {
    private static final Logger logger = LoggerFactory.getLogger(TxtAnalysis.class);

    public TxtAnalysis() {

    }

    @Override
    public void analysis(InputStream is, AnalysisListenerBase listener, AnalysisParam param) {
        InputStreamReader read = null;
        BufferedReader br = null;
        try {
            read = new InputStreamReader(is, param.getCharset());// 考虑到编码格式
            int rowCount = 0;
            int rowNum = 0;//行号，从1开始,初始为0
            //FileReader read = new FileReader(file); //不考虑转码，可使用FileReader;
            br = new BufferedReader(read);//使用缓冲流按行读取
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null && (param.getMaxRow() == 0 || rowCount < param.getMaxRow())) {
                rowNum++;//累计
                line = line.trim();
                if (!"".equals(line)) {
                    if(param.getSplitLimit()==-1){
                        if (param.getDelHeadRows() > param.getHeadRowCount()) {
                            param.getHeadRowCountAndAdd();
                            logger.info("ignore head:" + line);
                            handleHead(Arrays.asList(line),rowNum, listener, param);
                            continue;
                        }
                        rowCount++;
                        handleRow(Arrays.asList(line),rowNum, listener, param);//处理第一行
                    }else {
//                    line = new String(line.getBytes("ISO_8859_1"), "GBK");
//                    line = line.replace("，", ",");//中文的全角逗号，转换为半角
                        String strArr[] = line.split(param.getSplitRegex(), param.getSplitLimit());
                        if (strArr != null && strArr.length > 0) {
                            if (param.getDelHeadRows() > param.getHeadRowCount()) {
                                param.getHeadRowCountAndAdd();
                                logger.info("ignore head:" + Arrays.toString(strArr));
                                handleHead(Arrays.asList(strArr),rowNum, listener, param);//处理第一行
                                continue;
                            }
                            rowCount++;
                            handleRow(Arrays.asList(strArr),rowNum, listener, param);//处理第一行
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null) br.close();
                if (read != null) read.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            super.finish(listener, param);//文件读取完成
            listener.finish();//监听完成
        }

    }


}
